package com.easyj.base.security.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.stereotype.Component;
import com.easyj.base.security.OnlineUser;

@Component
public class JCacheSecurityService {
	
private @Autowired  CacheManager cacheManager;

   private final static String Captcha="Captcha";
   
   private final static String OnlineUser="OnlineUser";
	
	public void setCaptchaCode(String uuid,String text) {
		 Cache cache = cacheManager.getCache(Captcha);
		 cache.put(uuid, text);
	}
	
	public String getCaptchaCode(String uuid) {
		Cache cache = cacheManager.getCache(Captcha);
		return cache.get(uuid, String.class);
	  
	}

	
	public void setOnlineUser(String token,OnlineUser user) {
		Cache cache = cacheManager.getCache(OnlineUser);
		cache.put(token, user);
		
	}
	
	public OnlineUser getOnlineUser(String token) {
		Cache cache = cacheManager.getCache(OnlineUser);
		return cache.get(token, OnlineUser.class);
	 
	}
	
	//清除在线用户
	public void deleteOnlineUser(String token) {
		Cache cache = cacheManager.getCache(OnlineUser);
		cache.evictIfPresent(token);
	}
	
	
}
